﻿#include "FingerprintEnroll.h"
#include "FingerprintCacheDB.h"
#include "FingerprintReader.h"
#include <QDebug>
namespace ZKFP
{

static const int ENROLLCNT  = 3;

FingerprintEnroll::FingerprintEnroll(FingerprintCacheDB *cacheDB, QObject *parent)
    : QObject(parent),
      m_cacheDB(0),
      m_reader(0),
      m_enrollIdx(0),
      m_bRegister(false)
{
    m_cacheDB = cacheDB;
}

void FingerprintEnroll::setFingerprintCacheDB(FingerprintCacheDB *cacheDB)
{
    m_cacheDB = cacheDB;
}

void FingerprintEnroll::reset()
{
    if(m_cacheDB && m_reader)
    {
        disconnect(m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)),
                   this, SLOT(addFingerprint(QImage,QByteArray)));
    }
    m_enrollIdx = 0;
    m_bRegister = false;
    m_regTemplate.clear();
    m_fpTemplate[0].clear();
    m_fpTemplate[1].clear();
    m_fpTemplate[2].clear();

}

void FingerprintEnroll::beginEnroll(FingerprintReader * reader)
{
    reset();
    m_reader = reader;

    if(m_cacheDB && m_reader)
    {
        connect(m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)),
                this, SLOT(addFingerprint(QImage,QByteArray)));
    }
    m_reader->beginCapture();
}

void FingerprintEnroll::cancelEnroll()
{
    reset();
    m_reader->cancelCapture();
}

bool FingerprintEnroll::addFingerprint(QImage fingerImage, QByteArray fpTemplate)
{
    Q_UNUSED(fingerImage);
    return addFPTemplate(fpTemplate);
}

bool FingerprintEnroll::addFPTemplate(QByteArray fpTemplate)
{
    qDebug() << "m_enrollIdx = " << m_enrollIdx;
    if(m_cacheDB == 0)
    {
        return false;
    }
    if (m_enrollIdx >= ENROLLCNT)
    {
        //reset();
        return false;
    }
    if (m_enrollIdx > 0)
    {
        int score = m_cacheDB->verifyFinger(m_fpTemplate[m_enrollIdx - 1], fpTemplate);
        qDebug() << "score = " << score;
        if (score <= 0)
        {
            //m_enrollIdx = 0;
            //m_bRegister = false;
            emit fingerprintRejected();
            qDebug() << "fingerprintRejected()";
            return false;
        }
    }
    m_fpTemplate[m_enrollIdx] = fpTemplate;

    if (++m_enrollIdx >= ENROLLCNT)
    {
        int ret = m_cacheDB->mergeFinger(m_fpTemplate[0], m_fpTemplate[1], m_fpTemplate[2], m_regTemplate);
        //m_enrollIdx = 0;
        m_bRegister = FALSE;
        if (0 == ret)
        {
            emit enrollFinished(m_regTemplate);
            disconnect(m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)),
                this, SLOT(addFingerprint(QImage,QByteArray)));
            m_reader->cancelCapture();
            qDebug() << "enrollFinished()";
            return true;
        }
        else
        {
            emit enrollFailed();
            disconnect(m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)),
                this, SLOT(addFingerprint(QImage,QByteArray)));
            qDebug() << "enrollFailed()";
            return false;
        }
        if(m_reader)
        {
            disconnect(m_reader, SIGNAL(onFingerprintReceived(QImage,QByteArray)),
                this, SLOT(addFingerprint(QImage,QByteArray)));
        }
    }
    else
    {
        emit fingerprintAccepted();
        qDebug() << "fingerprintAccepted()";
        return true;
    }
    return false;
}

}
